package com.gimi.cloud.bbp.commonService.common.impl;

import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.commonService.common.CommonCoreNoticeService;
import com.gimi.cloud.bbp.commonService.redis.RedisService;
import com.gimi.cloud.bbp.commonService.sms.SmsPortal;
import com.gimi.cloud.bbp.commonService.sms.SmsService;
import com.gimi.cloud.bbp.dao.dao.core.MessageDao;
import com.gimi.cloud.bbp.dto.sys.AlidayuDTO;
import com.gimi.cloud.bbp.entity.core.Message;
import com.gimi.cloud.bbp.entity.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service(value = "commonCoreNoticeService")
@Transactional
public class CommonCoreNoticeServiceImpl implements CommonCoreNoticeService {
	private final Logger logger = LoggerFactory.getLogger(CommonCoreNoticeServiceImpl.class);

	private SmsPortal smsPortal;
	private SmsPortal[] smsPortalBackups;

	private MessageDao messageDao;
	private RedisService redisService;
	private SmsService smsService;

	@Autowired
	public CommonCoreNoticeServiceImpl(MessageDao messageDao, RedisService redisService, SmsService smsService) {
		this.messageDao = messageDao;
		this.redisService = redisService;
		this.smsService = smsService;
	}

	@Override
	public void sendNoticeNew( Map<String, Object> sendData) {
		User user = (User) sendData.get("user");
		String mobilePhone = (String)sendData.get("mobilePhone");
		String mail = (String)sendData.get("email");
		Integer userId = user.getUserId();
		if (mobilePhone == null || "".equals(mobilePhone)) {
			mobilePhone = user.getMobilePhone();
		}

		System.out.println("=====>>>>>mobilePhone " + mobilePhone);
		System.out.println("================================================================");
		sendData.remove("mobilePhone");
		sendData.remove("email");
		sendData.remove("user");
	}


	@Override
	public Boolean sendMessage(Message message,int type,String code,String noticeTypeNid) {
		Boolean flag = false;
		switch (type) {
			case SmsPortal.MESSAGE_SMS:
				flag = this.sendSms(message,code,noticeTypeNid);
				break;
			case SmsPortal.MESSAGE_EMAIL:
				//需要时自己拓展
				break;
			case SmsPortal.MESSAGE_MESSAGE:
				//需要时自己拓展
				break;
		}
		return flag;
	}


	private boolean sendSms(Message message,String code,String noticeTypeNid) {
		String mobile = message.getMobile();
		//判断手机号是否为空
		if(StringUtil.isBlank(mobile)){
			return false;
		}
		AlidayuDTO alidayuDTO = new AlidayuDTO();
		alidayuDTO.setPhone(mobile);
		alidayuDTO.setTemplate(noticeTypeNid);
		Map<String,String> params = new HashMap<>();
		params.put("code",code);
		params.put("product","买买分");
		alidayuDTO.setParams(JsonUtil.toJson(params));
		boolean result =false;
		StringBuffer sb = new StringBuffer();
		try{
			result = smsService.sendMsgByAliDayu(alidayuDTO);
		}catch(Exception e){
			logger.info("发送短信失败");
			e.printStackTrace();
		}
		if (result) {
			message.setStatus(1);
			sb.append("通过短信通道发送短信成功。");
			logger.info(sb.toString());
			message.setResult(sb.toString());
			messageDao.insert(message);
			return true;
		} else {
			message.setStatus(0);
			sb.append("通过短信通道发送短信失败");
			logger.error(sb.toString());
			message.setResult(sb.toString());
			messageDao.insert(message);
			return false;
		}
		
	}

}
